package com.harboursoftware.xstorage.results;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.FastDateFormat;

import com.harboursoftware.xstorage.model.XBucket;
import com.harboursoftware.xstorage.model.XUser;

public class GetServiceResult extends XMLResult {
    private XUser owner;
    private List<XBucket> buckets;
    //yyyy-MM-dd"T"kk:mm:ss.SSS"Z"
    private static String dateFormatPattern = "yyyy-MM-dd'T'kk:mm:ss.SSS'Z'";
    private static FastDateFormat dateFormat = FastDateFormat.getInstance(dateFormatPattern);
    
    public GetServiceResult() {}

    public GetServiceResult(XUser owner, List<XBucket> buckets) {
        this.owner = owner;
        this.buckets = buckets;
    }

    @Override
    public String toXML() {
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
            .append("<ListAllMyBucketsResult xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\">")
            .append("<Owner>")
            .append("<ID>").append(owner.getId()).append("</ID>")
            .append("<DisplayName>").append(owner.getDisplayName()).append("</DisplayName>")
            .append("</Owner>")
            .append("<Buckets>");
        
        for(XBucket bucket : buckets) {
            String creationDate = formatCreationDate(bucket.getCreationDate());
            strBuilder.append("<Bucket>")
                .append("<Name>").append(bucket.getName()).append("</Name>")
                .append("<CreationDate>").append(creationDate).append("</CreationDate>")
                .append("</Bucket>");
        }

        strBuilder.append("</Buckets>")
        .append("</ListAllMyBucketsResult>");
                
        
        return strBuilder.toString();
    }
    
    private String formatCreationDate(Date creationDate) {
        return dateFormat.format(creationDate);
    }

    public XUser getOwner() {
        return owner;
    }

    public void setOwner(XUser owner) {
        this.owner = owner;
    }

    public List<XBucket> getBuckets() {
        return buckets;
    }

    public void setBuckets(List<XBucket> buckets) {
        this.buckets = buckets;
    }
}
